package QueueTest;

import StackTest.EmptyException;

public class MyQueue {
    //单链表实现队列
    static class Node{
        public int val;
        public Node next;

        public Node(int val) {
            this.val = val;
        }

    }
    public Node head;
    public Node last;
    public int usedSize;

    //入队
    public void offer(int val){
        Node node = new Node(val);
        if(head == null){
            head = node;
            last = node;
        }else{
            last.next = node;
            last =node;
        }
        usedSize++;
    }
    //出队
    public int  poll(){
        if(head == null){
            throw new EmptyException("队列是空的！");
            //return -1;
        }
        int ret = head.val;
        head  = head.next;
        if(head == null){
            last = null; //只有一个节点，那么last也要置空
        }
        usedSize--;
        return ret;
    }

    public boolean isEmpty(){
        return usedSize == 0;
    }

    public int  peek(){
        if(head == null){
            throw new EmptyException("队列是空的！");
            //return -1;
        }
        int ret = head.val;
        return ret;
    }
    public int getUsedSize(){
        return usedSize;
    }

}
